package com.sci99.ex.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.sci99.ex.util.MyBatisUtil;

public class CourseAssignService {

	static Logger log = Logger.getLogger(CourseAssignService.class);

	public static void addDepartmentCourseAssign(Long cid, Long did)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> parameter = new HashMap<>(4, 1);
		parameter.put("cid", cid);
		parameter.put("did", did);
		s.insert("addDepartmentCourseAssign", parameter);
		s.commit();
		s.close();
	}

	public static void addCourseDptAssign(Long cid, List<Long> ids)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		try {
			Map<String, Object> parameter;
			for (Long did : ids) {
				parameter = new HashMap<>(4, 1);
				parameter.put("cid", cid);
				parameter.put("did", did);
				s.insert("addDepartmentCourseAssign", parameter);
			}
			s.commit();
		} catch (Exception e) {
			s.rollback();
			throw e;
		} finally {
			s.close();
		}
	}

	public static void delCourseDptAssign(Long cid, List<Long> ids)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		try {
			Map<String, Object> parameter;
			for (Long did : ids) {
				parameter = new HashMap<>(4, 1);
				parameter.put("cid", cid);
				parameter.put("did", did);
				s.delete("delDepartmentCourseAssign", parameter);
			}
			s.commit();
		} catch (Exception e) {
			s.rollback();
			throw e;
		} finally {
			s.close();
		}
	}

	public static void delDepartmentCourseAssign(Long cid, Long did)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> parameter = new HashMap<>(4, 1);
		parameter.put("cid", cid);
		parameter.put("did", did);
		s.delete("delDepartmentCourseAssign", parameter);
		s.commit();
		s.close();
	}

	/**
	 * 员工添加课程
	 * 
	 * @param cid
	 * @param eid
	 * @throws Exception
	 */
	public static void addEmployeeCourseAssign(Long cid, Long eid)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> parameter = new HashMap<>(4, 1);
		parameter.put("cid", cid);
		parameter.put("eid", eid);
		s.insert("addEmployeeCourseAssign", parameter);
		s.commit();
		s.close();
	}

	/**
	 * 员工批量添加课程
	 * 
	 * @param cids
	 * @param eid
	 * @throws Exception
	 */
	public static void addEmployeeCourseAssign(List<Long> cids, Long eid)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		List<Map<String, Object>> args = new ArrayList<>();
		for (Long cid : cids) {
			Map<String, Object> parameter = new HashMap<>(4, 1);
			parameter.put("cid", cid);
			parameter.put("eid", eid);
			args.add(parameter);
		}
		s.insert("addEmployeeCourseAssigns", args);
		s.commit();
		s.close();
	}

	public static void delEmployeeCourseAssign(Long cid, Long eid)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> parameter = new HashMap<>(4, 1);
		parameter.put("cid", cid);
		parameter.put("eid", eid);
		s.delete("delEmployeeCourseAssign", parameter);
		s.commit();
		s.close();
	}
}
